package com.stevenmoon.csdnplus.presenter;

import com.stevenmoon.csdnplus.app.Constants;
import com.stevenmoon.csdnplus.bean.headline.CommentInfoBean;
import com.stevenmoon.csdnplus.bean.headline.CommentListReqBean;
import com.stevenmoon.csdnplus.bean.headline.CommunityDetailBean;
import com.stevenmoon.csdnplus.bean.headline.DetailReqBean;
import com.stevenmoon.csdnplus.bean.headline.DetailUpDownInfo;
import com.stevenmoon.csdnplus.bean.headline.DetailUpDownReqBean;
import com.stevenmoon.csdnplus.bean.FavoriteOperationInfo;
import com.stevenmoon.csdnplus.bean.FavoriteOperationReqBean;
import com.stevenmoon.csdnplus.bean.FollowOperationInfo;
import com.stevenmoon.csdnplus.bean.FollowOperationReqBean;
import com.stevenmoon.csdnplus.bean.headline.HeadlineDetailBean;
import com.stevenmoon.csdnplus.bean.UserRelationBean;
import com.stevenmoon.csdnplus.logic.CommonManager;
import com.stevenmoon.csdnplus.logic.HeadlineManager;
import com.stevenmoon.csdnplus.logic.ProfileManager;
import com.stevenmoon.csdnplus.mvpview.HeadlineDetailView;
import com.stevenmoon.csdnplus.presenter.base.MvpBaseNoCheckPresenter;
import com.stevenmoon.csdnplus.util.CollectionUtil;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by WangJun on 2016/4/19.
 */
public class HeadlineDetailPresenter extends MvpBaseNoCheckPresenter<HeadlineDetailView> {

    private static class DetailInfo {
        public UserRelationBean userRelationBean;
        public HeadlineDetailBean headlineInfoBean;
        public CommunityDetailBean communityDetailBean;
        public CommentInfoBean commentInfoBean;
    }


    public void loadDetail(String username, final int articleId, final String url, final String type) {
        getView().showLoadingView();
        final DetailInfo info = new DetailInfo();
        Subscription s = HeadlineManager.getRelationWith(username)
                .concatMap(new Func1<UserRelationBean, Observable<HeadlineDetailBean>>() {
                    @Override
                    public Observable<HeadlineDetailBean> call(UserRelationBean userRelationBean) {
                        info.userRelationBean = userRelationBean;
                        DetailReqBean detailReqBean = new DetailReqBean();
                        detailReqBean.setId(articleId);
                        return HeadlineManager.getHeadlineDetail(detailReqBean);
                    }
                })
                .concatMap(new Func1<HeadlineDetailBean, Observable<CommunityDetailBean>>() {
                    @Override
                    public Observable<CommunityDetailBean> call(HeadlineDetailBean headlineInfoBean) {
                        info.headlineInfoBean = headlineInfoBean;
                        return HeadlineManager.getCommunityDetail(headlineInfoBean.getForum_id());
                    }
                })
                .concatMap(new Func1<CommunityDetailBean, Observable<DetailInfo>>() {
                    @Override
                    public Observable<DetailInfo> call(CommunityDetailBean communityDetailBean) {
                        info.communityDetailBean = communityDetailBean;
                        return Observable.just(info);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<DetailInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().hideLoadingView();
                        getView().showLoadDetailFail();
                    }

                    @Override
                    public void onNext(DetailInfo detailInfo) {
                        getView().showAuthorInfo(detailInfo.userRelationBean);
                        getView().showDetail(detailInfo.headlineInfoBean);
                        getView().showCommunityInfo(detailInfo.communityDetailBean);
                        getView().hideLoadingView();
                    }
                });
        getCompositeSubscription().add(s);
    }

    private int mCommentPage = 1;

    public void loadComments(String url, int articleId, String type){
        CommentListReqBean commentReq = new CommentListReqBean();
        commentReq.setPageno(1);
        //FIXME type有三种 hackernews，content_center(不知道在哪里用？)，和普通的原地址
        if(type.contains("hackernews")){
            commentReq.setUrl(Constants.CSDNURL.API_HACKERNEWS + articleId);
        }else{
            commentReq.setUrl(url);
        }
        Subscription s = HeadlineManager.getCommentList(commentReq)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new rx.Subscriber<CommentInfoBean>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        //TODO 评论加载失败的处理
                        //getView().showC(e.getMessage());
                    }

                    @Override
                    public void onNext(CommentInfoBean resultBean) {
                        getView().showNewComments(resultBean);
                        mCommentPage = 1;
                    }
                });
        getCompositeSubscription().add(s);
    }

    public void loadMoreComments(String url, int articleId, String type){
        CommentListReqBean commentReq = new CommentListReqBean();
        commentReq.setPageno(mCommentPage+1);
        //FIXME type有三种 hackernews，content_center(不知道在哪里用？)，和普通的原地址
        if(type.contains("hackernews")){
            commentReq.setUrl(Constants.CSDNURL.API_HACKERNEWS + articleId);
        }else{
            commentReq.setUrl(url);
        }
        Subscription s = HeadlineManager.getCommentList(commentReq)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new rx.Subscriber<CommentInfoBean>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        //TODO 评论加载失败的处理

                    }

                    @Override
                    public void onNext(CommentInfoBean resultBean) {
                        if(CollectionUtil.isEmpty(resultBean.getData())){
                            getView().showNoMoreComments();
                        }else{
                            getView().showMoreComments(resultBean);
                            mCommentPage++;
                        }

                    }
                });
        getCompositeSubscription().add(s);
    }

    public void doDetailUpDown(int articleId, String typeStr, int status){
        getView().showLoadingView();
        int type;
        if(typeStr.contains("hackernews")){
            type = DetailUpDownReqBean.TYPE_GEEK;
        }else{
            type = DetailUpDownReqBean.TYPE_NEWS;
        }
        DetailUpDownReqBean reqBean = new DetailUpDownReqBean();
        reqBean.setArticle_id(articleId).setStatus(status).setType(type);
        Subscription s = HeadlineManager.doDetailUpDown(reqBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new rx.Subscriber<DetailUpDownInfo>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().showUpArticleFail();
                        getView().hideLoadingView();
                    }

                    @Override
                    public void onNext(DetailUpDownInfo resultBean) {
                        getView().showUpArticleSuccess(resultBean);
                        getView().hideLoadingView();
                    }
                });
        getCompositeSubscription().add(s);
    }

    public void addFavorite(String url, String username, String title){
        getView().showLoadingView();
        FavoriteOperationReqBean reqBean = new FavoriteOperationReqBean();
        reqBean.setUrl(url).setUsername(username).setTitle(title);
        Subscription s = HeadlineManager.addFavorite(reqBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new rx.Subscriber<FavoriteOperationInfo>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().showCollectFail();
                        getView().hideLoadingView();
                    }

                    @Override
                    public void onNext(FavoriteOperationInfo resultBean) {
                        getView().showCollectSuccess(resultBean);
                        getView().hideLoadingView();
                    }
                });
        getCompositeSubscription().add(s);
    }

    public void deleteFavorite(String url, final String username, String title){
        getView().showLoadingView();
        FavoriteOperationReqBean reqBean = new FavoriteOperationReqBean();
        reqBean.setUrl(url).setUsername(username).setTitle(title);
        Subscription s = HeadlineManager.addFavorite(reqBean)
                .flatMap(new Func1<FavoriteOperationInfo, Observable<FavoriteOperationInfo>>() {
                    @Override
                    public Observable<FavoriteOperationInfo> call(FavoriteOperationInfo favoriteOperationInfo) {
                        FavoriteOperationReqBean reqBean = new FavoriteOperationReqBean();
                        reqBean.setId(favoriteOperationInfo.getData().getId()).setUsername(username);
                        return HeadlineManager.deleteFavorite(reqBean);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new rx.Subscriber<FavoriteOperationInfo>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().showUnCollectFail();
                        getView().hideLoadingView();
                    }

                    @Override
                    public void onNext(FavoriteOperationInfo resultBean) {
                        getView().showUnCollectSuccess(resultBean);
                        getView().hideLoadingView();
                    }
                });
        getCompositeSubscription().add(s);
    }

    public void doFollow(String username, String fans){
        getView().showLoadingView();
        FollowOperationReqBean reqBean = new FollowOperationReqBean();
        reqBean.setFans(fans).setUsername(username);
        Subscription s = CommonManager.doFollow(reqBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new rx.Subscriber<FollowOperationInfo>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().showAddFollowFail();
                        getView().hideLoadingView();
                    }

                    @Override
                    public void onNext(FollowOperationInfo resultBean) {
                        getView().showAddFollowSuccess(resultBean);
                        getView().hideLoadingView();
                    }
                });
        getCompositeSubscription().add(s);
    }

    public void unFollow(String username, String fans){
        getView().showLoadingView();
        FollowOperationReqBean reqBean = new FollowOperationReqBean();
        reqBean.setFans(fans).setUsername(username);
        Subscription s = CommonManager.unFollow(reqBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new rx.Subscriber<FollowOperationInfo>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().showCancelFollowFail();
                        getView().hideLoadingView();
                    }

                    @Override
                    public void onNext(FollowOperationInfo resultBean) {
                        getView().showCancelFollowSuccess(resultBean);
                        getView().hideLoadingView();
                    }
                });
        getCompositeSubscription().add(s);
    }

}
